import { evalEl, getFnObjData, sortKey } from './auth'
import { submitDealReq, evalElKeyInVal } from '@/utils/auth'
import { getOpenParamData } from '@/utils/hyopen'

import { getRemoteUrlParam } from '@/utils/hyurl'
import { initEnum } from '@/utils/hyenum'
import { changeFormKeyRules, resetNotRequiredRuleObj } from './hyformrules'
import { getGlobalParam } from './hyrecord'
import { inputChangeEventControlItemObj } from './hyctr'
import { initFormListWidth } from './hyscreenWidth'

export function testForm(_this) {
  console.log('000000000test Form000000')
  // changeLoadRemoteModel(_this,'ddfd')
}

export function changeFormModelVal(thisObj, record) {
  const tmpRecord = Object.assign({}, record)
  const setList = thisObj.formKey
  // thisObj.$nextTick(() => {
  const setVal = {}
  for (const i in setList) {
    const data = setList[i]
    if (data.tag === 'date') {
      setVal[data.key] = tmpRecord[data.key]
    } else {
      setVal[data.key] = tmpRecord[data.key]
    }
  }
  thisObj.formModel = Object.assign({}, thisObj.formModel, setVal)
  thisObj.globalData.globalParam = getGlobalParam(thisObj)
  changeInitFormEvent(thisObj, thisObj.globalData)
  thisObj.formModel = Object.assign({}, thisObj.formModel, setVal)
}

export function handleInit(thisObj) {
  const urlKey = 'init'
  const param = getRemoteUrlParam(thisObj, urlKey, thisObj.globalData)
  thisObj.loading = true
  submitDealReq(param).then((res) => {
    thisObj.loading = false
    if (res.data.success) {
      thisObj.initData = Object.assign({}, res.data.data, thisObj.openParam)
      changeFormModelVal(thisObj, thisObj.initData)
      initEnum(thisObj, thisObj.initData)
      // changeFormEnumKey(thisObj, thisObj.initData)
      thisObj.$message.success(res.data.message)
    } else {
      thisObj.$message.warning(res.data.message)
    }
  })
}

export function changeInitFormEvent(thisObj, globalData) {
  if (thisObj.formList.formEvent !== undefined) {
    for (const eventKey in thisObj.formList.formEvent) {
      const eventDataArr = thisObj.formList.formEvent[eventKey]
      for (const eventIdx in eventDataArr) {
        const eventData = eventDataArr[eventIdx]
        if (eventData.s_OBJ_EVENT === 'change') {
          inputChangeEventControlItemObj(thisObj, null, eventData, globalData)
        }
      }
    }
  }
}

export function changeShowActiveRowCol(thisObj, S_RULE_TPL_KEY, globalData) {
  changeActiveRowColVisual(thisObj, S_RULE_TPL_KEY, globalData, 1)
}

export function changeHiddenActiveRowCol(thisObj, S_RULE_TPL_KEY, globalData) {
  changeActiveRowColVisual(thisObj, S_RULE_TPL_KEY, globalData, 2)
}

export function changeActiveRowColVisual(thisObj, S_RULE_TPL_KEY, globalData, defaultShow) {
  const formList = Object.assign({}, thisObj.initUiVal[thisObj.initFormKey])
  const tmpList = formList.colData[S_RULE_TPL_KEY]
  const tmpRowCollapseKey = {}
  let rowActive = {}
  let colActive = {}
  if (tmpList === undefined) {
    return
  }
  const rowKey = tmpList[0].rowKey
  const rowData = formList.rowData[rowKey]
  const collaseKey = rowData.collaseKey
  const colKey = S_RULE_TPL_KEY
  const activeRowCol = changeFormRowActiveDataByKey(thisObj, globalData, rowKey, colKey, defaultShow)
  console.debug('activeRowCol', activeRowCol)
  // 字段一个场景呈现,一个场景隐藏
  const changeRowKey = activeRowCol.changeRowKey
  for (const c in changeRowKey) {
    const colRowKey = changeRowKey[c]
    changeOldColActiveData(thisObj, colRowKey, colKey)
    const tmpCollaseKey = getCollaseKeyListNewKey(thisObj, collaseKey, activeRowCol, colRowKey)
    tmpRowCollapseKey[collaseKey] = tmpCollaseKey
  }
  if (changeRowKey.length === 0) {
    // 表示当前字段不符合规则,若存在呈现字段则隐藏
    const colRowKey = rowKey
    changeOldColActiveData(thisObj, colRowKey, colKey)
    changeOldFormRowCollapseKey(thisObj, rowKey, colKey)
  }
  rowActive = Object.assign({}, rowActive, activeRowCol.rowActive)
  colActive = Object.assign({}, colActive, activeRowCol.colActive)
  thisObj.formRowCollapseKey = Object.assign({}, thisObj.formRowCollapseKey, tmpRowCollapseKey)
  thisObj.formColActiveData = Object.assign({}, thisObj.formColActiveData, colActive)
  thisObj.formRowActiveData = Object.assign({}, thisObj.formRowActiveData, rowActive)
}

export function changeFormRowActiveDataByKey(thisObj, globalData, rowKey, colKey, defaultShow) {
  let appListKey = []
  let appFormData = {}
  if (thisObj.initUiVal.sessionData !== undefined && thisObj.initUiVal.sessionData.sessionForm !== undefined) {
    if (thisObj.initUiVal.sessionData.sessionForm.listKey.length > 0) {
      appListKey = thisObj.initUiVal.sessionData.sessionForm.listKey
      appFormData = thisObj.initUiVal.sessionData.sessionForm.listData
    }
  } else if (thisObj.initUiVal.appData !== undefined && thisObj.initUiVal.appData.appForm !== undefined) {
    // 存在定制应用,以定制应用配置为主
    if (thisObj.initUiVal.appData.appForm.listKey.length > 0) {
      appListKey = thisObj.initUiVal.appData.appForm.listKey
      appFormData = thisObj.initUiVal.appData.appForm.listData
    }
  }
  const activeRowCol = {}
  const changeRowKey = []
  const formList = Object.assign({}, thisObj.initUiVal[thisObj.initFormKey])
  const tmpFormRowActiveData = Object.assign({})
  const tmpFormColActiveData = {}
  const rowData = Object.assign({}, formList.rowData[rowKey])
  const colList = rowData.colList
  for (const c in colList) {
    const colItem = colList[c]
    if (defaultShow !== null && colKey !== null) {
      if (colKey === colItem.model) {
        colItem.defaultShow = defaultShow
      }
    }
    if (thisObj.modalFnShow[colItem.model] !== undefined && !thisObj.modalFnShow[colItem.model]) {
      continue
    }
    if (colItem.defaultShow !== 1) {
      // 非显示模型时,隐藏
      continue
    }
    if (appListKey.length > 0 && appListKey.indexOf(colItem.model) < 0) {
      // 应用定义不存在时,隐藏
      continue
    }
    const tmpColList = formList.colData[colItem.model]
    // colList 标签有可能分布在多行(互斥呈现)
    let newColData = null
    for (const tc in tmpColList) {
      const tmpCol = tmpColList[tc]

      const el = evalEl(tmpCol.objEl, globalData.globalParam)
      if (el) {
        const elKey = evalElKeyInVal(tmpCol.elKey, tmpCol.elKeyInVal, globalData.globalParam)
        if (elKey) {
          newColData = Object.assign({}, tmpCol)
          break
        }
      }
    }

    if (newColData !== null && tmpFormColActiveData[colItem.model] === undefined) {
      // colList 标签有可能分布在多行(互斥呈现),所以列的行数据返回
      // 变量名字一样时,只取第一次满足条件字段
      // 有符合条件字段数据，才添加行与列关系
      const colRowKey = newColData.rowKey
      if (tmpFormRowActiveData[colRowKey] !== undefined) {
        const tmpRowData = tmpFormRowActiveData[colRowKey]
        tmpRowData.colList.push(colItem)
      } else {
        changeRowKey.push(colRowKey)
        const tmpRowData = Object.assign({}, formList.rowData[colRowKey])
        tmpRowData.colList = []
        tmpRowData.colList.push(colItem)
        tmpFormRowActiveData[colRowKey] = tmpRowData
      }

      if (appListKey.length > 0) {
        if (newColData.objEl === undefined && appFormData[colItem.model] !== undefined) {
          // 简单条件才替换标签,复杂的不处理
          newColData.label = appFormData[colItem.model].label
        }
      }
      tmpFormColActiveData[colItem.model] = newColData
    }
  }
  activeRowCol.changeRowKey = changeRowKey
  activeRowCol.rowActive = tmpFormRowActiveData
  activeRowCol.colActive = tmpFormColActiveData
  return activeRowCol
}

export function changeFormRowActiveData(thisObj, globalData) {
  const formList = Object.assign({}, thisObj.initUiVal[thisObj.initFormKey])
  changeCollapseSrcRowActiveData(thisObj, globalData, 'collapseForm')
  changeCollapseSrcRowActiveData(thisObj, globalData, 'noCollapseKey')
  thisObj.collapseKey = formList.collapseKey
}

export function changeCollapseSrcRowActiveData(thisObj, globalData, collapseSrc) {
  const formRowCollapseTmpKey = {}
  const formList = Object.assign({}, thisObj.initUiVal[thisObj.initFormKey])
  let tmpFormRowActiveData = Object.assign({})
  let tmpFormColActiveData = {}
  const mergeActive = Object.assign([], formList.collapseKey[collapseSrc])
  for (const i in mergeActive) {
    const collapseRowList = []
    const collapse = mergeActive[i]
    const rowList = formList.collapseData[collapse.key]
    for (const r in rowList) {
      const rowItem = rowList[r]
      const rowKey = rowItem.rowKey
      const activeRowCol = changeFormRowActiveDataByKey(thisObj, globalData, rowKey, null, null)
      if (activeRowCol.rowActive[rowKey] === undefined) {
        continue
      }
      if (activeRowCol.rowActive[rowKey].rowKey !== undefined) {
        collapseRowList.push(rowItem)
      }
      tmpFormRowActiveData = Object.assign({}, tmpFormRowActiveData, activeRowCol.rowActive)
      tmpFormColActiveData = Object.assign({}, tmpFormColActiveData, activeRowCol.colActive)
    }
    formRowCollapseTmpKey[collapse.key] = sortKey(collapseRowList, 'rowNum')
  }
  thisObj.formRowCollapseKey = Object.assign({}, thisObj.formRowCollapseKey, formRowCollapseTmpKey)
  thisObj.formRowActiveData = Object.assign({}, thisObj.formRowActiveData, tmpFormRowActiveData)
  thisObj.formColActiveData = Object.assign({}, thisObj.formColActiveData, tmpFormColActiveData)
}

/**
 * 变更当前数据
 * @param thisObj
 * @param colRowKey
 * @param colKey
 */
export function changeOldColActiveData(thisObj, colRowKey, colKey) {
  const oldColActiveData = thisObj.formColActiveData[colKey]
  if (oldColActiveData !== undefined) {
    // 存在旧数据
    const oldRowKey = oldColActiveData.rowKey
    if (colRowKey !== oldRowKey) {
      changeOldFormRowCollapseKey(thisObj, oldRowKey, colKey)
    }
  }
}

export function changeOldFormRowCollapseKey(thisObj, oldRowKey, colKey) {
  const oldRowActiveData = thisObj.formRowActiveData[oldRowKey]
  console.debug('old row key', oldRowKey, colKey)
  if (oldRowActiveData === undefined) {
    return
  }
  const oldCollaseKey = oldRowActiveData.collaseKey
  const oldcollaseKeyList = thisObj.formRowCollapseKey[oldCollaseKey]
  const oldTmpColList = getColListNotEqColKey(oldRowActiveData.colList, colKey)
  thisObj.formRowActiveData[oldRowKey].colList = oldTmpColList
  if (oldTmpColList.length === 0) {
    const oldTmpCollaseKey = getCollaseKeyListNotEqKey(oldcollaseKeyList, oldRowKey)
    thisObj.formRowCollapseKey[oldCollaseKey] = oldTmpCollaseKey
  }
}

export function getCollaseKeyListNewKey(thisObj, collaseKey, activeRowCol, colRowKey) {
  const tmpCollaseKey = []
  const collaseKeyList = thisObj.formRowCollapseKey[collaseKey]
  let hasRowKey = false
  for (const i in collaseKeyList) {
    const tmp = collaseKeyList[i]
    tmpCollaseKey.push(tmp)
    if (tmp.rowKey === colRowKey) {
      hasRowKey = true
    }
  }
  if (!hasRowKey) {
    const tmpRow = {}
    tmpRow.collaseKey = activeRowCol.rowActive[colRowKey].collaseKey
    tmpRow.gutter = activeRowCol.rowActive[colRowKey].gutter
    tmpRow.rowKey = activeRowCol.rowActive[colRowKey].rowKey
    tmpRow.rowType = activeRowCol.rowActive[colRowKey].rowType
    tmpRow.rowNum = activeRowCol.rowActive[colRowKey].rowNum
    tmpCollaseKey.push(tmpRow)
  }
  return sortKey(tmpCollaseKey, 'rowNum')
}

export function getCollaseKeyListNotEqKey(collaseKeyList, key) {
  const oldTmpCollaseKey = []
  // collaseKeyList 要移除该行记录
  for (const oldIdx in collaseKeyList) {
    const oldTmp = collaseKeyList[oldIdx]
    if (oldTmp.rowKey !== key) {
      oldTmpCollaseKey.push(oldTmp)
    }
  }
  return oldTmpCollaseKey
}

export function getColListNotEqColKey(colList, colKey) {
  const oldTmpColList = []
  const oldColList = colList
  for (const oldIdx in oldColList) {
    const tmp = oldColList[oldIdx]
    if (tmp.model !== colKey) {
      oldTmpColList.push(tmp)
    }
  }
  return oldTmpColList
}

export function initFormFnParam(thisObj, fnFlag) {
  // 一开始初始化读写参数
  const formKey = thisObj.initUiVal[thisObj.initFormKey].formKey
  const objTmpFn = {}
  for (const idx in formKey) {
    const key = formKey[idx].key
    objTmpFn[key] = fnFlag
  }
  thisObj.modalFn = Object.assign({}, objTmpFn)
  changeFormFnParam(thisObj)
}
export function changeFormFnParam(thisObj) {
  const srcLabel = thisObj.initSrcObj.initSrcLabel
  // 设置初始加载表单读写参数
  if (thisObj.initUiVal.modalFn !== undefined && thisObj.initUiVal.modalFn[srcLabel] !== undefined) {
    if (thisObj.initUiVal.modalFn[srcLabel].form !== undefined) {
      const formKey = thisObj.initUiVal[thisObj.initFormKey].formKey
      thisObj.modalFnShow = Object.assign({}, {})
      thisObj.modalFn = Object.assign({}, {})
      for (const idx in formKey) {
        const key = formKey[idx].key
        changeFormKeyFnParam(thisObj, key, srcLabel)
      }
    }
  }
}

export function changeFormKeyFnParam(thisObj, key) {
  const srcLabel = thisObj.initSrcObj.initSrcLabel
  if (thisObj.initUiVal.modalFn !== undefined && thisObj.initUiVal.modalFn[srcLabel] !== undefined) {
    if (thisObj.initUiVal.modalFn[srcLabel].form !== undefined) {
      const modalDataFn = thisObj.initUiVal.modalFn[srcLabel].form
      if (modalDataFn !== undefined) {
        const dataList = modalDataFn[key]
        const tmpObj = {}
        const tmpObjShow = {}
        tmpObjShow[key] = true
        if (dataList === undefined) {
          // 没配置 读写
          tmpObj[key] = true
        } else {
          // const globalData = {}
          const elObj = getFnObjData(thisObj, dataList, thisObj.globalData, true)
          // const elObj = getProcessElTrueData(thisObj, key, dataList, thisObj.formModel, true)
          // if (elObj[key]) {
          if (elObj.elKey) {
            // 配置的时只读属性
            tmpObj[key] = false
          } else {
            tmpObj[key] = true
          }
          if (elObj.editFlag !== undefined && elObj.editFlag === 3) {
            // 配置为隐藏
            tmpObjShow[key] = false
          }
        }
        const fnShow = Object.assign({}, tmpObjShow)
        thisObj.modalFnShow = Object.assign({}, thisObj.modalFnShow, fnShow)
        const cccc = Object.assign({}, thisObj.modalFn, tmpObj)
        thisObj.modalFn = Object.assign({}, cccc)
        if (!cccc[key]) {
          // 只读模式时,设置所有校验规则为非必填,去除标签前面红色*号
          resetNotRequiredRuleObj(thisObj, key)
        } else {
          changeFormKeyRules(thisObj, key)
        }
      } else {
        // 没有配置
        // initFormFnParam(thisObj, false)
      }
    }
  }
}

export function getFormModelKeyData(thisObj) {
  const setList = thisObj.formKey
  const setVal = {}
  for (const i in setList) {
    const data = setList[i]
    if (data.tag === 'date') {
      setVal[data.key] = thisObj.formModel[data.key]
    } else {
      setVal[data.key] = thisObj.formModel[data.key]
    }
  }
  return setVal
}

export function initForm(thisObj) {
  // 一开始初始化按钮
  const formList = thisObj.initUiVal[thisObj.initFormKey]
  thisObj.formList = initFormListWidth(thisObj, formList)
  if (thisObj.formList.formEvent !== null) {
    thisObj.formEvent = Object.assign({}, thisObj.formList.formEvent)
  }
  if (thisObj.formList.formKey !== null) {
    thisObj.formKey = Object.assign({}, thisObj.formList.formKey)
  }
  if (thisObj.initUiVal.formList !== null) {
    thisObj.initLoadObj.loadFormId = thisObj.initUiVal.formList.loadFormId
  }

  changeFormRowActiveData(thisObj, thisObj.globalData)
}

export function changeForm(thisObj, triggerObj, openParam) {
  // 一开始每次加载按钮
  const formList = thisObj.initUiVal[thisObj.initFormKey]
  thisObj.formList = initFormListWidth(thisObj, formList)
  if (thisObj.formList.formEvent !== null) {
    thisObj.formEvent = Object.assign({}, thisObj.formList.formEvent)
  }
  if (thisObj.formList.formKey !== null) {
    thisObj.formKey = Object.assign({}, thisObj.formList.formKey)
  }
}

export function initFormValue(thisObj) {
  // 一开始初始化读写参数
  const formKey = thisObj.initUiVal[thisObj.initFormKey].formKey
  const objTmpFn = {}
  for (const idx in formKey) {
    const key = formKey[idx].key
    objTmpFn[key] = undefined
  }
  thisObj.formModel = Object.assign({}, objTmpFn)
}
export function changeFormValue(thisObj, objData) {
  // 获取开窗数据进行初始化
  if (thisObj.initUiVal[thisObj.initFormKey] !== undefined) {
    const formKey = thisObj.initUiVal[thisObj.initFormKey].formKey
    for (const idx in formKey) {
      const key = formKey[idx].key
      changeFormKeyVal(thisObj, key, objData)
    }
  }
}
export function changeFormKeyVal(thisObj, key, objData) {
  const eventSetVal = thisObj.initUiVal[thisObj.initFormKey].eventSetVal
  if (eventSetVal !== undefined) {
    const dataList = eventSetVal[key]
    if (dataList === undefined || dataList.length === 0) {
      // 没配置,不进行赋值
      return
    } else {
      const setTmpData = getOpenParamData(objData, dataList)
      if (setTmpData[key] !== undefined) {
        const cccc = Object.assign({}, thisObj.formModel, setTmpData)
        thisObj.formModel = Object.assign({}, cccc)
      }
    }
  }
}

export function setFormTriggerObjVal(thisObj, triggerObj) {
  const I_VAL_TYPE = triggerObj.i_VAL_TYPE
  const S_RULE_TPL_KEY = triggerObj.s_RULE_TPL_KEY
  const S_RULE_TPL_VAL = triggerObj.s_RULE_TPL_VAL
  const tmpVal = {}
  if (I_VAL_TYPE === 1) {
    tmpVal[S_RULE_TPL_KEY] = Number(S_RULE_TPL_VAL)
  } else {
    tmpVal[S_RULE_TPL_KEY] = S_RULE_TPL_VAL
  }
  const cccc = Object.assign({}, thisObj.formModel, tmpVal)
  thisObj.formModel = Object.assign({}, cccc)
}

export function clearFormKeyVal(thisObj, key, val) {
  const tmpVal = {}
  tmpVal[key] = val
  const cccc = Object.assign({}, thisObj.formModel, tmpVal)
  const clearInitData = Object.assign({}, thisObj.globalData.initData, tmpVal)
  thisObj.formModel = Object.assign({}, cccc)
  thisObj.globalData.formModel = Object.assign({}, cccc)
  thisObj.globalData.initData = Object.assign({}, clearInitData)
  thisObj.globalData.globalParam = getGlobalParam(thisObj)
}
